package com.baizhi.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.annotation.DeleteCacheAnnotation;
import com.baizhi.dao.BannerDao;
import com.baizhi.entity.Banner;
import com.baizhi.service.BannerService;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BannerServiceImpl implements BannerService {
    private Logger logger= LoggerFactory.getLogger(BannerServiceImpl.class);

    @Autowired
    private BannerDao bannerDao;
    @Override
    @AddCacheAnnotation
    public Map selectAllBanner(int page, int limit) {
        int offset=(page-1)*limit;
        List<Banner> list=bannerDao.selectAllBanner(offset,limit);
        int count=bannerDao.selectCount();
        Map map=new HashMap();
        System.out.println("创建Map");
        logger.debug("创建Map");
        logger.info("创建Map");
        logger.warn("创建Map");
        logger.error("创建Map");
        map.put("count",count);
        map.put("data",list);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

    @Override
    @DeleteCacheAnnotation
    public Map deleteBanner(int bannerId) {
        int count =bannerDao.deleteBanner(bannerId);
        Map map =new HashMap();

        if(count>0){
            map.put("isDelete",true);
        }else{
            map.put("isDelete",false);
        }

        return map;
    }

    @Override
    @DeleteCacheAnnotation
    public Map insertBanner(Banner banner) {
        Map map=new HashMap();
        int count =bannerDao.insertBanner(banner);
        if(count>0){
            map.put("isAdd",true);
        }else{
            map.put("isAdd",false);
        }
        return map;
    }

    @Override
    @AddCacheAnnotation
    public Banner selectByBannerId(int bannerId) {
        return bannerDao.selectByBannerId(bannerId);
    }

    @Override
    @DeleteCacheAnnotation
    public Map updateBanner(Banner banner) {

        Map map=new HashMap();
        int count =bannerDao.updateBanner(banner);
        if(count>0){
            map.put("isUpdate",true);
        }else{
            map.put("isUpdate",false);
        }
        return map;
    }

    @Override
    public Map updateBanner1(Banner banner) {
        Map map=new HashMap();
        int count =bannerDao.updateBanner1(banner);
        if(count>0){
            map.put("isUpdate",true);
        }else{
            map.put("isUpdate",false);
        }
        return map;
    }

   /* @Override
    @DeleteCacheAnnotation
    public Map updateBannerStates(Banner banner) {
        int count=bannerDao.updateBannerStates(banner);
        Map map=new HashMap();
        System.out.println(banner.getBannerId()+"编号");
        System.out.println(banner.getBannerState()+"状态");
        if(count>0){
            map.put("isUpdates","更改成功");
        }else{
            map.put("isUpdates","更改失败");
        }
        return map;
    }*/

    @Override
    @AddCacheAnnotation
    public Map selectAlls() {
        List list = bannerDao.selectAlls();
        Workbook workbook= ExcelExportUtil.exportExcel(new ExportParams("轮播图数据","banner"),Banner.class,list);

        try {
            workbook.write(new FileOutputStream("E:\\资料\\项目资料\\后期项目\\5 poi - day4"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map map=new HashMap();
        map.put("data",list);
        if(map!=null){
            map.put("isxiazai","导出成功");

        }else{
            map.put("isxiazai","导出失败");
        }

        return map;
    }

    @Override
    @DeleteCacheAnnotation
    public Map updates(Banner banner) {
        if(banner.getBannerState()==2){
            banner.setBannerState(1);
        }else{
            banner.setBannerState(2);
        }
        Map map=new HashMap();
        int count=bannerDao.updates(banner);

        System.out.println(banner.getBannerState()+"状态");
        System.out.println(banner.getBannerId()+"编号");
        if(count>0){
            map.put("isUpdates","更改成功");
        }else{
            map.put("isUpdates","更改失败");
        }
        return map;
    }

    @Override
    public List<Banner> selectBannerByCount(int count) {
        return bannerDao.selectBannerByCount(count);
    }
}
